package org.lanqiao.tjut.Test;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.Test;
import org.lanqiao.tjut.db.DBDriverFactory;
import org.lanqiao.tjut.entity.StinforEntity;


public class Teacher {
	Scanner input=new Scanner(System.in);
	@Test
	public void doLog() {
		String id;
		int psw;
//登录
		do {
			System.out.println("请输入账号(若要返回上一级，请输入返回)：");
			id=input.next();
			if(id.equals("返回")){
				Sys s=new Sys();
				s.chose();
			}
			System.out.println("请输入密码：");
			psw=input.nextInt();
		} while (!(id.equals("admin")&& psw==123456));
		   System.out.println("登陆成功");
	}
	@Test
	public void teInterface() {
		//教师界面
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println("$        1.添加学生信息                                        ");
        System.out.println("$        2.查询学生信息                                        ");
        System.out.println("$        3.修改学生信息                                        ");
        System.out.println("$        4.删除学生信息                                        ");
        System.out.println("$        0.任意键退出                                            ");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
	}
	@Test
	public void doAdd() {
		// TODO Auto-generated method stub
		System.out.println("-----------1.添加学生信息 -------------");
		System.out.print("请输入学生账号:");
		int a=input.nextInt();
		System.out.print("学生密码默认为123:");
		int b=123;
		System.out.print("请输入学生姓名:");
		String c=input.next();
		System.out.print("请输入学生性别:");
		String d=input.next();
        System.out.print("请输入学生年龄:");
        int e=input.nextInt();
        System.out.print("请输入学生电话:");
        String f=input.next();
        System.out.print("请输入学生地址:");
        String g=input.next();
        System.out.print("学生默认昵称为无");
        String h="无";
        //将信息插入表中
        String strSQL="insert into stinfor(s_id,s_psw,s_name,s_sex,s_age,s_telephone,s_address,s_nickname) values(?,?,?,?,?,?,?,?)";
        int i=DBDriverFactory.getDBDriverInstance().update(strSQL,new Object[]{a,b,c,d,e,f,g,h});
        System.out.println(i>0?"学生信息已添加":"添加失败");
        String str="insert into s"+a+"(s_id,s_psw,s_name,s_sex,s_age,s_telephone,s_address,s_nickname) values(?,?,?,?,?,?,?,?)";
        int j=DBDriverFactory.getDBDriverInstance().update(str,new Object[]{a,b,c,d,e,f,g,h});
        System.out.println(j>0?"开通好友功能成功":"开通好友功能失败");
        //将学生信息插入一个名为friend的表中,因查询好友资料不方便,已废弃
//        String strSQL0="insert into friend (s_id) values(?)";
//        int j=DBDriverFactory.getDBDriverInstance().update(strSQL0,new Object[]{a});
//        System.out.println(j>0?"学生信息已添加":"添加失败");
        //显示该学生信息
        String strSQL1="select * from stinfor where s_id=?";
		   List lst=(List)DBDriverFactory.getDBDriverInstance().query(strSQL1,new MapListHandler(),new Object[]{a});
		   System.out.println("该学生的信息为:");
		   printList(lst);
        
	}
	@Test
	public void doDelete() {
		//删除
        System.out.println("------------4.删除学生信息--------------");
		String strSQL="select * from stinfor";
		List lst=(List)DBDriverFactory.getDBDriverInstance().query(strSQL,new BeanListHandler(StinforEntity.class));
	       printList(lst);
	    System.out.println("请输入要删除学生的id:");
	    int id=input.nextInt();
	    String strSQL1="DELETE FROM stinfor WHERE s_id=?";
	    int i=DBDriverFactory.getDBDriverInstance().update(strSQL1,new Object[]{id});
	    System.out.println(i>0?"删除成功":"删除失败");
	    for(int a=1;a<=9;a++){
	    	String strSQL2="DELETE FROM s"+a+" WHERE s_id=?";
		    int j=DBDriverFactory.getDBDriverInstance().update(strSQL2,new Object[]{id});
		    System.out.println(i>0?"删除成功":"删除失败");
	    }
	}
	@Test
	public void doChange() {
		//修改
		System.out.println("----------3.修改学生信息------------");
		String strSQL="select * from stinfor";
		List lst=(List)DBDriverFactory.getDBDriverInstance().query(strSQL,new BeanListHandler(StinforEntity.class));
	    printList(lst);
	    System.out.print("请输入您要修改的id:");
	    int id=input.nextInt();
	    System.out.println("1.学生帐号(s_id)");
	    System.out.println("2.学生密码(s_psw)");
	    System.out.println("3.学生姓名(s_name)");
		System.out.println("4.学生性别(s_sex)");
        System.out.println("5.学生年龄(s_age)");
        System.out.println("6.学生电话(s_telephone)");
        System.out.println("7.学生地址(s_address)");
        System.out.print("请输入您要修改的项目(1.2.3.4.5.6.7):"); 
        int i=input.nextInt();
        switch (i) {
		case 1:
			System.out.print("请输入新帐号:");
			int a=input.nextInt();
			String str1="update stinfor set s_id=? where s_id="+id;
		    int a1=DBDriverFactory.getDBDriverInstance().update(str1,new Object[]{a});
	        System.out.println(a1>0?"修改成功":"修改失败");
			break;
		case 2:
		    int b;
		    int b0;
			do{System.out.print("请输入新密码:");
			b=input.nextInt();
			System.out.print("请再输入一遍:");
			b0=input.nextInt();}while(b!=b0);
			String str2="update stinfor set s_psw=? where s_id="+id;
		    int b1=DBDriverFactory.getDBDriverInstance().update(str2,new Object[]{b});
	        System.out.println(b1>0?"修改成功":"修改失败");
			break;
		case 3:
			System.out.print("请输入新姓名:");
			String c=input.next();
			String str3="update stinfor set s_name=? where s_id="+id;
		    int c1=DBDriverFactory.getDBDriverInstance().update(str3,new Object[]{c});
	        System.out.println(c1>0?"修改成功":"修改失败");
			break;
		case 4:
			System.out.print("请输入新性别:");
			String d=input.next();
			String str4="update stinfor set s_sex=? where s_id="+id;
		    int d1=DBDriverFactory.getDBDriverInstance().update(str4,new Object[]{d});
	        System.out.println(d1>0?"修改成功":"修改失败");
			break;
		case 5:
			System.out.print("请输入新年龄:");
			int e=input.nextInt();
			String str5="update stinfor set s_age=? where s_id="+id;
		    int e1=DBDriverFactory.getDBDriverInstance().update(str5,new Object[]{e});
	        System.out.println(e1>0?"修改成功":"修改失败");
			break;
		case 6:
			System.out.print("请输入新电话:");
			String f=input.next();
			String str6="update stinfor set s_telephone=? where s_id="+id;
		    int f1=DBDriverFactory.getDBDriverInstance().update(str6,new Object[]{f});
	        System.out.println(f1>0?"修改成功":"修改失败");
			break;
		case 7:
			System.out.print("请输入新地址:");
			String g=input.next();
			String str7="update stinfor set s_address=? where s_id="+id;
		    int g1=DBDriverFactory.getDBDriverInstance().update(str7,new Object[]{g});
	        System.out.println(g1>0?"修改成功":"修改失败");
			break;

		default:
			break;
		}
	}
	@Test
	public void doQuery(){
		System.out.println("--------2.查询学生信息------------");
	    System.out.println("a.查询所有学生信息");
	    System.out.println("b.根据指定编号查询单个学生信息");
	    System.out.println("c.根据姓名模糊查询学生信息");
		System.out.println("d.根据地址模糊查询学生信息");
		System.out.println("e.根据年龄段查询学生信息");
		System.out.print("请输入您要进行的操作(a.b.c.d.e):");
		char i=input.next().charAt(0);
		switch (i) {
		case 'a':
			System.out.println("a.查询所有学生信息");
			String strA="select * from stinfor";
		List lst1=(List)DBDriverFactory.getDBDriverInstance().query(strA,new BeanListHandler(StinforEntity.class));
		printList(lst1);
			break;
		case 'b':
			System.out.println("b.根据指定编号查询单个学生信息");
			System.out.println("请输入学生账号:");
			int b1=input.nextInt();
			String strB="select * from stinfor where s_id=?";
		   List lst2=(List)DBDriverFactory.getDBDriverInstance().query(strB,new MapListHandler(),new Object[]{b1});
	       printList(lst2);
	       break;
		case 'c':
			 System.out.println("c.根据姓名模糊查询学生信息");
			System.out.println("请输入要查询的字段:");
			String c1=input.next();
			String strC="select * from stinfor where s_name like ?";
		   List lst3=(List)DBDriverFactory.getDBDriverInstance().query(strC,new MapListHandler(),new Object[]{"%"+c1+"%"});
	       printList(lst3);
	       break;
		case 'd':
			 System.out.println("d.根据地址模糊查询学生信息");
				System.out.println("请输入要查询的字段:");
				String d1=input.next();
				String strD="select * from stinfor where s_address like ?";
			   List lst4=(List)DBDriverFactory.getDBDriverInstance().query(strD,new MapListHandler(),new Object[]{"%"+d1+"%"});
		       printList(lst4);
		       break;
		case 'e':
			 System.out.println("e.根据年龄段查询学生信息");
				System.out.println("请输入最小年龄:");
				int e1=input.nextInt();
				System.out.println("请输入最大年龄:");
				int e2=input.nextInt();
				String strE="select * from stinfor where s_age>="+e1+" and s_age<="+e2;
			   List lst5=(List)DBDriverFactory.getDBDriverInstance().query(strE,new MapListHandler());
		       printList(lst5);
		       break;
		default:
			break;
		}
	}
	
	private void printList(List lst) {
		
		
		for(int i=0;i<lst.size();i++){
			System.out.println(lst.get(i).toString());
		}
}
}
